import Vue from "vue";
import Router from "vue-router";
// import Home from "../views/Home.vue"
const Home = ()=>import("../views/Home.vue");
// import Reg from "../views/Reg.vue"
// import Login from "../views/Login.vue"
// const Login = ()=>import("../views/Login.vue");

// import GoodsDetail from "../views/GoodsDetail.vue"
// import My from "../views/My.vue"
const GoodsDetail = ()=>import(/* webpackChunkName:'demot' */ "../views/GoodsDetail.vue");
const My = ()=>import(/* webpackChunkName:'demot' */ "../views/My.vue");

import ShoppingCar from "../views/ShoppingCar.vue"
import Category from "../views/Category";
import Phone from "../views/Category/Phone";
import FamilyElec from "../views/Category/FamilyElec";
import MouthRed from "../views/Category/MouthRed";
import NotFound from "../views/NotFound.vue"


// 把插件安装到Vue里
Vue.use(Router);

let router = new Router({
    mode:"history",
    routes:[
        {
            path:"/",
            redirect:"/home"
        },
        {
            path:"/home",
            component:Home
            // component:resolve=>require(["../views/Home.vue"],resolve)
        },
        {
            path:"/Reg",
            // component:Reg
            component:resolve=>require(["../views/Reg.vue"],resolve)
        },
        {
            path:"/Login",
            component:()=>import("../views/Login.vue")
        },
        {
            name:"GoodsDetail",
            path:"/GoodsDetail/:goodsid",
            component:GoodsDetail,
            props:true
        },
        {
            name:"My",
            path:"/My",
            component:My,
            meta:{
                requiresAuth:true
            }
        },
        {
            path:"/ShoppingCar",
            component:ShoppingCar,
            meta:{
                requiresAuth:true
            }
            // beforeEnter(to,from,next){
            //     console.log("/ShoppingCar的路由独享守卫");
            //     if(!sessionStorage.getItem("username")){
            //         next("/Login?path="+to.path);
            //     }else{
            //         next();
            //     }
            // }
        },
        {
            path:"/Category",
            component:Category,
            children:[
                {
                    path:"Phone",
                    component:Phone
                },
                {
                    path:"FamilyElec",
                    component:FamilyElec
                },
                {
                    path:"MouthRed",
                    component:MouthRed
                }
            ]
        },
        {
            path:"*",
            component:NotFound
        }
    ]
});

// 全局前置钩子。
router.beforeEach((to,from,next)=>{
    console.log("全局前置的钩子函数");
    console.log("to",to);
    console.log("from",from);
    // if(to.path==="/ShoppingCar" || to.path=="/My" || to.path==""){
    if(to.meta.requiresAuth){
        // if(没有登录){
        if(!sessionStorage.getItem("username")){
            next("/Login?path="+to.path);
        }
    }
    next();
});


// 全局后置钩子
router.afterEach(()=>{
    console.log("全局后置的钩子函数");
})

export default  router;